package cn.edu.henu.team.pageplug;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import cn.edu.henu.team.R;
import cn.edu.henu.team.bean.books;

public class MyGridViewAdapter extends BaseAdapter {
   private ArrayList<books> arr = new ArrayList<books>();
   private Context mContext;
   private TextView bookid;
   //实现搜索
   private MyFilter filter; //创建MyFilter对象
   private FilterListener listener = null; //接口对象

   public MyGridViewAdapter(Context mContext){
      this.mContext=mContext;
   };
   public MyGridViewAdapter(ArrayList<books> arr, Context mContext,FilterListener listener) {
      this.arr = arr;
      this.mContext = mContext;
      this.listener=listener;
   }

   @Override
   public int getCount() {
      return arr.size();
   }

   @Override
   public Object getItem(int position) {
      return arr.get(position);
   }

   @Override
   public long getItemId(int position) {
      return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder = null;
      if(convertView == null){
         //填写ListView的图标和标题等控件的来源，来自于layout_list_item这个布局文件
         //把控件所在的布局文件加载到当前类中
         convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_book_item,null);
         bookid = convertView.findViewById(R.id.mybookid);
         //生成一个ViewHolder的对象
         holder = new ViewHolder();
         //获取控件对象
         holder.Grid_imageview=convertView.findViewById(R.id.bookImage);
         holder.Grid_textview=convertView.findViewById(R.id.bookName);
         convertView.setTag(holder);

      }else{
         holder = (ViewHolder) convertView.getTag();
      }
      bookid.setText(arr.get(position).getSource());
      //修改空间属性
      holder.Grid_textview.setText(arr.get(position).getName());
      //加载第三方网络图片W
      RequestOptions requestOptions = new RequestOptions()
              .placeholder(R.drawable.book_default)
              .error(R.drawable.book_default)
              .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
      Glide.with(mContext).load(arr.get(position).getImage()).apply(requestOptions).centerCrop().into(holder.Grid_imageview);
      return convertView;
   }
   public Filter getFilter() {
      if (filter == null) {
         filter = new MyFilter(arr);
      }
      return filter;
   }
   /**
    * 创建内部类MyFilter继承Filter类，并重写相关方法，实现数据的过滤
    */
   class MyFilter extends Filter {
      //创建集合保存原始数据
      private ArrayList<books> original = new ArrayList<>();

      public MyFilter(ArrayList<books> original) {
         this.original = original;
      }

      //该方法返回搜索过滤后的数据
      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
         //创建FilterResults对象
         FilterResults filterResults = new FilterResults();
         /**
          * 没有搜索内容的话就还是给filterResults赋值原始数据的值和大小
          * 执行了搜索的话，根据搜索规则过滤即可，最后把过滤后的数据的值和大小赋值给filterResults
          */
         if (TextUtils.isEmpty(constraint)) {
            //取出当前的数据源的值和集合元素个数
            //此时返回的filterResults就是原始的数据，不进行过滤
            filterResults.values = original;
            filterResults.count = original.size();
         } else {
            ArrayList<books> mList = new ArrayList<>();
            //创建集合保护过滤后的数据
            for (books s : original) {
               //这里的toLowerCase():是将字符串中的字母全部变为小写，而非字母则不做改变
               if (s.getName().trim().toLowerCase().contains(constraint.toString().trim().toLowerCase())) {
                  //规则匹配的话就往集合中添加该数据
                  mList.add(s);
               }

            }
            filterResults.values = mList;
            filterResults.count = mList.size();
         }
         return filterResults;
      }

      //该方法用来刷新用户界面，根据过滤后的数据重新展示列表
      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
         //获取过滤后的数据
         arr = (ArrayList<books>) results.values;
         //如果接口对象不为空，那么调用接口中的方法获取过滤后的数据，具体的实现在new这个接口的时候重写的方法里执行
         if (listener != null) {
            listener.getFilterData(arr);
         }
         //刷新数据源显示
         //通知数据观察者当前所关联的数据源已经发生改变，任何与该数据有关的视图都应该去刷新自己。
         notifyDataSetChanged();
      }
   }

   public interface FilterListener{
      void getFilterData(List<books> list);
   }



   public static class ViewHolder{
      public ImageView Grid_imageview;
      public TextView Grid_textview;
   }
}
